import { createApp } from 'vue';
import Message from './Message.vue';
export type MessageType = 'success' | 'error' | 'default';

const createMessage = (message: string, type: MessageType, timeout = 2000): void => {
  const messageInstance = createApp(Message, {
    message,
    type,
  });
  const mountNode = document.createElement('div');

  document.body.appendChild(mountNode);
  messageInstance.mount(mountNode);

  // 这里记得清除定时器呀！！
  let timer: number | null = setTimeout(() => {
    // 课程中的使用有误！！
    messageInstance.unmount();
    document.body.removeChild(mountNode);
    timer && clearTimeout(timer);
    timer = null;
  }, timeout);
};

export default createMessage;
